<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27
 * Time: 9:56
 */
namespace Modules;

use Admin\Models\bpm\XHBpmPlan;
use Admin\Models\bpm\XHBpmTransList;
use Admin\Models\crm\XhcustInfo;
use Helpers\Main;
use Helpers\XhException;
use Phalcon\Di;

class ProcessPlan
{
    /**
     * 获取流程计划枚举
     *
     * @param null $custId
     *
     * @return array
     *
     * create by dale  20181127
     */
    public static function getPlanName($custId = null)
    {
        $planArr = Di::getDefault()->get('modelsManager')->createBuilder()
            ->columns("p.iBPPId,p.iCUId,p.tCreateDate,p.iCreateUserId,p.bFinish,c.sSchoolName,t.tRunItemDate,pt.tRealDate,t.sClasses")
            ->from(['p' => 'Admin\Models\bpm\XHBpmPlan'])
            ->leftJoin('Admin\Models\crm\XhcustInfo', 'c.iCUId = p.iCUId', 'c')
            ->leftJoin('Admin\Models\bpm\XHBpmTransList', 'p.iBPPId = t.iBPPId', 't')
            ->leftJoin('Admin\Models\bpm\XHBpmPlanContent', "p.iBPPId = pt.iBPPId and pt.sNodeName = '上线时间'", 'pt')
            ->where(' p.bDelete = 0 ')
            ->orderBy('pt.tRealDate DESC');
        if ($custId) {
            $planArr->andWhere('p.iCUId= :iCUId:', ['iCUId' => $custId]);
        }
        $planArr = $planArr->getQuery()->execute()->toArray();
        Main::runUserNameById($planArr, ['iCreateUserId' => 'iCreateUserId'], true);
        $planEnums = [];
        foreach ($planArr as $plan) {
            $item = [
                'id'   => $plan['iBPPId'],
                'name' => "{$plan['sSchoolName']}"
            ];
//            if ($plan['tRunItemDate']) {
//                $item['name'] .= '上项目时间:' . $plan['tRunItemDate'];
//            }
            if ($plan['tRealDate']) {
                $item['name'] .= ' 上线:' . $plan['tRealDate'];
            }
            if ($plan['sClasses']) {
                $item['name'] .= " " . $plan['sClasses'];
            }
            $plan['tCreateDate'] = date('Y-m-d', strtotime($plan['tCreateDate']));
            $item['name'] .= " ({$plan['iCreateUserId']})" ;
            switch ($plan['bFinish']) {
                case 0:
                    $item['name'] = '【进行中】' . $item['name'];
                    break;
                case 1:
                    $item['name'] = '【完成】' . $item['name'];
                    break;
                default:
                    break;
            }
            $planEnums[] = $item;
        }
        return $planEnums;
    }

    /**
     * 根据学校id获取流程计划
     *
     * @param $schoolId
     *
     * @return array
     * @throws XhException
     *
     * create by dale  20181127
     */
    public static function getPlanNameBySchoolId($schoolId)
    {
        if (!$schoolId) {
            throw new XhException('没有传入学校id');
        }
        $custId = Customer::getCustId($schoolId);

        return self::getPlanName($custId);
    }

    /**
     * 获取计划数据
     *
     * @param array $customerId
     *
     * @return array
     *
     * create by qgz 20181218
     */
    public static function getPlanData($customerId = [])
    {
        $params = ['conditions' => 'bDelete = 0 and bFinish = 0'];
        if ($customerId) {
            $params['conditions'] .= ' and iCUId in ({idArr:array})';
            $params['bind'] = ['idArr' => array_values(array_filter(array_unique($customerId)))];
        }
        return XHBpmPlan::find($params)->toArray();
    }

    /**
     * 根据学校id获取计划数据
     *
     * @param $schoolId
     *
     * @return array
     * @throws XhException
     *
     * create by qgz 20181218
     */
    public static function getPlanDataBySchoolId($schoolId)
    {
        if (!$schoolId) {
            throw new XhException('没有传入学校id');
        }
        $customerId = Customer::getCustIdArr($schoolId);
        $custInfo = self::getPlanData(array_values($customerId));
        if (!$custInfo) {
            return [];
        }
        $return = [];
        foreach ($customerId as $schoolId => $customer) {
            $return[$schoolId] = [];
            foreach ($custInfo as $value) {
                if (isset($value['iCUId']) && $value['iCUId'] == $customer) {
                    $return[$schoolId][] = $value;
                }
            }
        }
        return $return;
    }
}
